package TestExercises08;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;

//算法水题
/*
请使用代码实现计算你活了多少天，用JDK7和JDK8两种方式完成
 */
public class Test04 {
    public static void main(String[] args) throws ParseException {

        //JDK7
        method1();

        //JDK8
        method2();
    }

    //JDK7
    private static void method1() throws ParseException {
        //规则：只要对时间进行计算或者判断，都需要先获取当前时间的毫秒值
        //1、计算出生年月日的毫秒值
        String birthday = "2002年1月13日";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = sdf.parse(birthday);
        long birthdayTime = date.getTime();
        //2、获取当前时间的毫秒值
        long todayTime = System.currentTimeMillis();
        //3、计算间隔时间
        long time = todayTime - birthdayTime;
        System.out.println(time / 1000 / 60 / 60 / 24);
    }

    //JDK8
    private static void method2() {
        LocalDate birthday = LocalDate.of(2002, 1, 13);
        LocalDate today = LocalDate.now();
        System.out.println(ChronoUnit.DAYS.between(birthday, today));
    }
}
